using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Library_management_system
{
    public partial class ReaderManagementForm : Form
    {
        public ReaderManagementForm()
        {
            InitializeComponent();
            LoadReaders();
        }

        private void LoadReaders(string keyword = "")
        {
            try
            {
                using (SqlConnection conn = DatabaseConfig.GetConnection())
                {
                    conn.Open();
                    string sql = "SELECT ReaderID, Name, Gender, BirthDate, Phone, Email, Address FROM Readers";
                    if (!string.IsNullOrWhiteSpace(keyword))
                    {
                        sql += " WHERE Name LIKE @kw OR Phone LIKE @kw OR Email LIKE @kw OR Address LIKE @kw";
                    }
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    if (!string.IsNullOrWhiteSpace(keyword))
                    {
                        cmd.Parameters.AddWithValue("@kw", "%" + keyword + "%");
                    }
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dgvReaders.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ReaderEditForm editForm = new ReaderEditForm();
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadReaders();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvReaders.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的读者！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int readerId = Convert.ToInt32(dgvReaders.SelectedRows[0].Cells["ReaderID"].Value);
            ReaderEditForm editForm = new ReaderEditForm(readerId);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadReaders();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvReaders.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的读者！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int readerId = Convert.ToInt32(dgvReaders.SelectedRows[0].Cells["ReaderID"].Value);
            if (MessageBox.Show("确定要删除该读者吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    using (SqlConnection conn = DatabaseConfig.GetConnection())
                    {
                        conn.Open();
                        string sql = "DELETE FROM Readers WHERE ReaderID=@id";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        cmd.Parameters.AddWithValue("@id", readerId);
                        cmd.ExecuteNonQuery();
                    }
                    LoadReaders();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadReaders(txtSearch.Text.Trim());
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtSearch.Text = string.Empty;
            LoadReaders();
        }
    }
}